package com.ngovanhau.justsort.database;

import java.util.ArrayList;
import java.util.List;

import com.ngovanhau.justsort.model.Level;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

	public final static String DB_NAME = "justsort2014.db";
	public final static int DB_VERSION = 1;

	public final static String TABLE_HIGHSCORE = "highscore";
	public final static String COLUMN_HIGH_LEVEL = "level";
	public final static String COLUMN_HIGH_TYPE = "type";
	public final static String COLUMN_HIGH_ROW = "row";
	public final static String COLUMN_HIGH_COLUMN = "column";
	public final static String COLUMN_HIGH_TIME = "time";

	public DatabaseHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String SQL_CREATE_DB = "CREATE TABLE " + TABLE_HIGHSCORE + "("
				+ COLUMN_HIGH_LEVEL + " INTEGER," + COLUMN_HIGH_TYPE
				+ " INTEGER PRIMARY KEY," + COLUMN_HIGH_ROW + " INTEGER,"
				+ COLUMN_HIGH_COLUMN + " INTEGER," + COLUMN_HIGH_TIME
				+ " INTEGER)";
		db.execSQL(SQL_CREATE_DB);
		// initData();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_HIGHSCORE);
		onCreate(db);
	}

	public void initData() {
		this.addHighScore(new Level(1, 1, 3, 3, 0));
		this.addHighScore(new Level(1, 2, 3, 4, 0));
		this.addHighScore(new Level(1, 3, 4, 3, 0));
		this.addHighScore(new Level(1, 4, 3, 5, 0));
		this.addHighScore(new Level(1, 5, 5, 3, 0));

		this.addHighScore(new Level(2, 6, 4, 4, 0));
		this.addHighScore(new Level(2, 7, 3, 6, 0));
		this.addHighScore(new Level(2, 8, 6, 3, 0));
		this.addHighScore(new Level(2, 9, 4, 5, 0));
		this.addHighScore(new Level(2, 10, 5, 4, 0));
		this.addHighScore(new Level(2, 11, 3, 7, 0));
		this.addHighScore(new Level(2, 12, 7, 3, 0));
		this.addHighScore(new Level(2, 13, 4, 6, 0));
		this.addHighScore(new Level(2, 14, 6, 4, 0));

		this.addHighScore(new Level(3, 15, 5, 5, 0));
		this.addHighScore(new Level(3, 16, 4, 7, 0));
		this.addHighScore(new Level(3, 17, 7, 4, 0));
		this.addHighScore(new Level(3, 18, 5, 6, 0));
		this.addHighScore(new Level(3, 19, 6, 5, 0));
		this.addHighScore(new Level(3, 20, 5, 7, 0));
		this.addHighScore(new Level(3, 21, 7, 5, 0));

		this.addHighScore(new Level(4, 22, 6, 6, 0));
		this.addHighScore(new Level(4, 23, 6, 7, 0));
		this.addHighScore(new Level(4, 24, 7, 6, 0));

		this.addHighScore(new Level(5, 25, 7, 7, 0));
		
		this.addHighScore(new Level(6, 26, 5, 5, 0));
	}

	public List<Level> getListHighScore() {
		List<Level> list = new ArrayList<Level>();

		SQLiteDatabase db = this.getReadableDatabase();
		String query = "SELECT * FROM " + TABLE_HIGHSCORE + " ORDER BY "
				+ COLUMN_HIGH_TYPE + " ASC";
		Cursor cursor = db.rawQuery(query, null);

		if (cursor.moveToFirst()) {
			do {
				Level item = new Level();
				item.setLevel(Integer.parseInt(cursor.getString(0)));
				item.setType(Integer.parseInt(cursor.getString(1)));
				item.setRow(Integer.parseInt(cursor.getString(2)));
				item.setColumn(Integer.parseInt(cursor.getString(3)));
				item.setTime(Integer.parseInt(cursor.getString(4)));
				list.add(item);
			} while (cursor.moveToNext());
		}
		return list;
	}

	public Level getHighScore(int level) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_HIGHSCORE, new String[] {
				COLUMN_HIGH_LEVEL, COLUMN_HIGH_TYPE, COLUMN_HIGH_ROW,
				COLUMN_HIGH_COLUMN, COLUMN_HIGH_TIME }, COLUMN_HIGH_TYPE
				+ " = ?", new String[] { String.valueOf(level) }, null, null,
				null);
		if (cursor != null) {
			cursor.moveToFirst();
		}
		Level item = new Level(Integer.parseInt(cursor.getString(0)),
				Integer.parseInt(cursor.getString(1)), Integer.parseInt(cursor
						.getString(2)), Integer.parseInt(cursor.getString(3)),
				Integer.parseInt(cursor.getString(4)));
		db.close();
		return item;
	}

	public void addHighScore(Level item) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(COLUMN_HIGH_LEVEL, item.getLevel());
		values.put(COLUMN_HIGH_TYPE, item.getType());
		values.put(COLUMN_HIGH_ROW, item.getRow());
		values.put(COLUMN_HIGH_COLUMN, item.getColumn());
		values.put(COLUMN_HIGH_TIME, item.getTime());

		db.insert(TABLE_HIGHSCORE, null, values);
		db.close();
	}

	public void updateHighScore(Level item) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(COLUMN_HIGH_LEVEL, item.getLevel());
		values.put(COLUMN_HIGH_TYPE, item.getType());
		values.put(COLUMN_HIGH_ROW, item.getRow());
		values.put(COLUMN_HIGH_COLUMN, item.getColumn());
		values.put(COLUMN_HIGH_TIME, item.getTime());

		db.update(TABLE_HIGHSCORE, values, COLUMN_HIGH_TYPE + " = ?",
				new String[] { String.valueOf(item.getType()) });
		db.close();
	}

	public void deleteHighScore(int level) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_HIGHSCORE, COLUMN_HIGH_TYPE + " = ?",
				new String[] { String.valueOf(level) });
		db.close();
	}
}